import "reflect-metadata";
import {
    createTestingConnections,
    closeTestingConnections,
} from "../../utils/test-utils";
import { Connection } from "../../../src";
import { User } from "./entity/UserEntity";

import { expect } from "chai";

describe("github issues > #8370 Add support for Postgres GENERATED ALWAYS AS IDENTITY", () => {
    let connections: Connection[];
    before(
        async () =>
            (connections = await createTestingConnections({
                entities: [User],
                schemaCreate: false,
                dropSchema: true,
                enabledDrivers: ["postgres"],
            }))
    );
    after(() => closeTestingConnections(connections));

    it("should produce proper SQL for creating a column with `BY DEFAULT` identity column", () =>
        Promise.all(
            connections.map(async (connection) => {
                const sqlInMemory = await connection.driver
                    .createSchemaBuilder()
                    .log();
                expect(sqlInMemory)
                    .to.have.property("upQueries")
                    .that.is.an("array")
                    .and.has.length(1);

                // primary key
                expect(sqlInMemory.upQueries[0])
                    .to.have.property("query")
                    .that.contains(
                        `"id" integer GENERATED ALWAYS AS IDENTITY NOT NULL`
                    );

                // second id
                expect(sqlInMemory.upQueries[0])
                    .to.have.property("query")
                    .that.contains(
                        `"secondId" bigint GENERATED ALWAYS AS IDENTITY NOT NULL`
                    );

                // third id
                expect(sqlInMemory.upQueries[0])
                    .to.have.property("query")
                    .that.contains(
                        `"thirdId" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL`
                    );

                // fourth id
                expect(sqlInMemory.upQueries[0])
                    .to.have.property("query")
                    .that.contains(
                        `"fourthId" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL`
                    );
            })
        ));
});
